package m.jclecai.com.view.common;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import m.jclecai.com.utils.BaseUtils;

/**
 * Created by andy on 2017/6/27.
 * 这里我们要做的一个操作就是显示
 */

public class AutoHeightViewPager extends ViewPager {


    private int mCurrentPosition;
    private int mCurrentPosition1;

    public AutoHeightViewPager(Context context) {
        this(context,null);
    }

    public AutoHeightViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);


        //预加载两个ViewPager
        setOffscreenPageLimit(6);

  /*      setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //这里的话，我们需要做的一个操作就是动态的改变ViewPager的高度；这里的话，我们是需要计算高度，
                measureItem(position, positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });*/
    }


    int[] mHeights=new int[6];
    int mCurrentPagerHeight=0;

    public void measureItem(int position, float positionOffset) {
        int originalHeight=0;
        int targetHeight=0;

        originalHeight = mHeights[position];
        targetHeight = mHeights[position+1];


        if (positionOffset!=0) {
            //这里表示正在移动
            System.out.println("ViewPager:右移动");  //position是变小的
            Float evaluate = evaluate(positionOffset, originalHeight, targetHeight);
            mCurrentPagerHeight=evaluate.intValue();

        }else{
            mCurrentPagerHeight=originalHeight;
        }
        requestLayout();
        System.out.println("ViewPager:position:"+position+" positionOffset: "+positionOffset+" mCurrentPostion: ");

        System.out.println("original:height:"+originalHeight
                +" height:"+targetHeight
                +" mCurrenPagerHeight"+mCurrentPagerHeight);
    }


   @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int maxHeight=0;
        int makeMeasureSpec = MeasureSpec.makeMeasureSpec((1 << 30) - 1, MeasureSpec.AT_MOST);
        for (int i=0;i<getChildCount();i++){
            View child = getChildAt(i);
            if(child instanceof ListView){
                ListView listView = (ListView) child;
                //int height = measureListViewWrongHeight(listView);
                int height = measureListViewWrongHeight(listView);

                child.measure(widthMeasureSpec,MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY));
                mHeights[i]=child.getMeasuredHeight();
            }else{
                child.measure(widthMeasureSpec,makeMeasureSpec);
                int measuredHeight = child.getMeasuredHeight();
                mHeights[i]=measuredHeight;
            }
            maxHeight=maxHeight>mHeights[i]?maxHeight:mHeights[i];
        }
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),maxHeight+BaseUtils.dip2px(getContext(),85));

        System.out.println("height:"+getMeasuredHeight());
        //super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(maxHeight+255,MeasureSpec.EXACTLY));
    }


    /**
     * 固执器：计算差值的一个方法；
     * @param fraction
     * @param startValue
     * @param endValue
     * @return
     */
    public static Float evaluate(float fraction, Number startValue, Number endValue) {
        float startFloat = startValue.floatValue();
        return startFloat + fraction * (endValue.floatValue() - startFloat);
    }


    int topHeight=0;

    // 动态改变listView的高度
    public int measureListViewWrongHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        int measureSpec = MeasureSpec.makeMeasureSpec((1 << 30) - 1, MeasureSpec.AT_MOST);
        if (listAdapter == null) {
            return 0;
        }
        int totalHeight = 0;
        int count = listAdapter.getCount();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(measureSpec, measureSpec);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        topHeight = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        params.height = topHeight;
        listView.setLayoutParams(params);
        return totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    }

}